package com.jingyanzi.marketing.service.impl;

import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jingyanzi.marketing.dao.GiveMapper;
import com.jingyanzi.marketing.domain.GiveDo;
import com.jingyanzi.marketing.entity.MaGive;
import com.jingyanzi.marketing.entity.MaGiveExample;
import com.jingyanzi.marketing.entity.MaGiveExample.Criteria;
import com.jingyanzi.marketing.query.GiveQuery;
import com.jingyanzi.marketing.result.ResultDO;
import com.jingyanzi.marketing.result.ResultSupport;
import com.jingyanzi.marketing.service.IGiveService;
import com.jingyanzi.marketing.util.BeanUtilsExtends;

@Service("maGiveService")
public class GiveServiceImpl extends BaseServiceImpl implements IGiveService {

	@Autowired
	public GiveMapper giveMapper;

	public ResultDO add(GiveDo maGiveDo) {
		MaGive give = new MaGive();
		ResultSupport result = BeanUtilsExtends.copy(give, maGiveDo);
		if (!result.isSuccess()) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "对象转换失败");
			result.setSuccess(false);
			return result;
		}
		give.setActivityOption("充" + maGiveDo.getRecharge() + "送" + maGiveDo.getAmountGive());
		give.setIsDelete(1);
		give.setCreatetime(Calendar.getInstance().getTime());
		int r = -1;
		try {
			r = giveMapper.insert(give);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "添加赠送信息失败");
			result.setSuccess(false);
			return result;
		}
		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "添加赠送信息失败");
			result.setSuccess(false);
			return result;
		}
		result.setModel(ResultDO.FIRST_MODEL_KEY, give.getId());
		return result;
	}

	public ResultDO modifi(GiveDo maGiveDo) {
		MaGive give = new MaGive();
		ResultSupport result = BeanUtilsExtends.copy(give, maGiveDo);
		if (!result.isSuccess()) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "对象转换失败");
			result.setSuccess(false);
			return result;
		}
		give.setActivityOption("充" + maGiveDo.getRecharge() + "送" + maGiveDo.getAmountGive());
		int r = -1;
		try {
			r = giveMapper.updateByPrimaryKeySelective(give);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "修改赠送信息失败");
			result.setSuccess(false);
			return result;
		}
		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "修改赠送信息失败");
			result.setSuccess(false);
			return result;
		}
		return result;
	}

	public ResultDO remove(Integer id) {
		MaGive give = new MaGive();
		ResultSupport result = new ResultSupport();
		if (!result.isSuccess())
			return result;
		give.setId(id);
		give.setIsDelete(-1);
		int r = -1;
		try {
			r = giveMapper.updateByPrimaryKeySelective(give);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "删除赠送信息失败");
			result.setSuccess(false);
			return result;
		}
		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "删除赠送信息失败");
			result.setSuccess(false);
			return result;
		}
		return result;
	}

	public ResultDO get(Integer id) {
		MaGive give = new MaGive();
		ResultSupport result = new ResultSupport();
		if (!result.isSuccess())
			return result;

		try {
			give = giveMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取赠送信息失败");
			result.setSuccess(false);
			return result;
		}
		if (give == null) {
			result.setErrorCode(ResultDO.GIVE_GET_ERROR);
			result.setErrorMsg("不存在该赠送信息");
			result.setSuccess(false);
			return result;
		}
		GiveDo maGiveDo = getMaGiveDo(give);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, maGiveDo);
		return result;
	}

	public ResultDO query(GiveQuery maGiveQuery) {
		ResultSupport result = new ResultSupport();

		List<MaGive> list = null;

		MaGiveExample example = new MaGiveExample();

		Criteria createCriteria = example.createCriteria();
		if (maGiveQuery.getDiscountId() != null) {
			createCriteria.andDiscountIdEqualTo(maGiveQuery.getDiscountId());
		}

		if (maGiveQuery.getActivityId() != null) {
			createCriteria.andActivityIdEqualTo(maGiveQuery.getActivityId());
		}
		if (maGiveQuery.getPresentId() != null) {
			createCriteria.andPresentIdEqualTo(maGiveQuery.getPresentId());
		}
		createCriteria.andIsDeleteEqualTo(1);

		List<GiveDo> giveDos = null;

		if (maGiveQuery.isPage()) {
			example.setLimitByPage(maGiveQuery.getStart() + "," + maGiveQuery.getPageRows());
			long count = giveMapper.countByExample(example);
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
		}

		try {
			list = giveMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取赠送信息失败");
			result.setSuccess(false);
			return result;
		}

		giveDos = getMaGiveDoList(list);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, giveDos);

		return result;
	}

}
